package com.example.pkmedical.Activity.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextClock;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;

import java.io.DataOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DataChangeActivity extends BaseActivity {

    @BindView(R.id.id_changetime_date)
    TextClock idChangetimeDate;
    @BindView(R.id.user_ok_bt)
    Button userOkBt;
    @BindView(R.id.user_ok_cancle)
    Button userOkCancle;
    TimePickerView pvTime;
    Date mFinallyData;
    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_datachange);
        ButterKnife.bind(this);

    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        mainHouse.setImageResource(R.drawable.img_title_sz);
        mainHouseText.setText(R.string.title_routine);
        idChangetimeDate.setFormat24Hour("yyyy / MM / dd HH:mm");
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.id_changetime_date, R.id.user_ok_bt, R.id.user_ok_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_changetime_date:
                DateSelector();
                break;
            case R.id.user_ok_bt:
                if (null != mFinallyData){
                    testDate(getTime(mFinallyData));
                }

                break;
            case R.id.user_ok_cancle:
                DataChangeActivity.this.finish();
                break;
        }
    }

    //时间选择器
    private void DateSelector() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        //正确设置方式 原因：注意事项有说明
        startDate.set(2020, 0, 1);
        endDate.set(2037, 11, 31);
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                idChangetimeDate.setText(getTimeText(date));
                mFinallyData = date;
//                timeText.setText(getTime(date));
//                Log.d("yangshuo", getTime(date));

            }
        })
                .setType(new boolean[]{true, true, true, true, true, false})
                .setContentTextSize(23)
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .build();

        pvTime.show();

    }

    public void testDate(String datetime) {
        try {
            Process process = Runtime.getRuntime().exec("su");
//            String datetime = "20210524.084200"; //测试的设置的时间【时间格式 yyyyMMdd.HHmmss】
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("setprop persist.sys.timezone GMT\n");
            os.writeBytes("/system/bin/date -s " + datetime + "\n");
            os.writeBytes("clock -w\n");
            os.writeBytes("exit\n");
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd.HHmmss");
        return format.format(date);
    }

    private String getTimeText(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy / MM / dd HH:mm");
        return format.format(date);
    }
}
